//
//  SmsCloudAearHistoryView.m
//  smsCloud
//
//  Created by 吴斌 on 2024/7/20.
//

#import "SmsCloudAearHistoryView.h"
#import "SmsCloudAearHistoryCollectionCell.h"
@interface SmsCloudAearHistoryView()<UICollectionViewDelegate,UICollectionViewDataSource>
@end
@implementation SmsCloudAearHistoryView
-(instancetype)init{
    if (self == [super init]) {
        [self smsCloudInitView];
    }
    return self;
}
- (void)smsCloudInitView{
    UILabel * smsCloudHistoryHeadLab = [[UILabel alloc] init];
    smsCloudHistoryHeadLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:32] weight:UIFontWeightRegular];
    smsCloudHistoryHeadLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#222222"];
    smsCloudHistoryHeadLab.text = @"历史记录";
    [self addSubview:smsCloudHistoryHeadLab];
    [smsCloudHistoryHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:23]));
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:35]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:45]));
    }];
    UIButton * smsCloudHistoryDelectBtn = [[UIButton alloc] init];
    [smsCloudHistoryDelectBtn setBackgroundImage:[SmsCloudHead SmsCloudImageNamed:@"smsCloudSearchDelect"] forState:UIControlStateNormal];
    [smsCloudHistoryDelectBtn addTarget:self action:@selector(smsCloudHistoryDelect) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:smsCloudHistoryDelectBtn];
    [smsCloudHistoryDelectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:34]));
        make.centerY.equalTo(smsCloudHistoryHeadLab);
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:44]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:44]));
    }];
    
    UICollectionViewFlowLayout* smsCloudHistoryFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    smsCloudHistoryFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    smsCloudHistoryFlowLayout.sectionInset = UIEdgeInsetsMake([SmsCloudHead SmsCloudScreewRatio:0], [SmsCloudHead SmsCloudScreewRatio:24], [SmsCloudHead SmsCloudScreewRatio:0],[SmsCloudHead SmsCloudScreewRatio:24]);
    self.smsCloudHistoryCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:smsCloudHistoryFlowLayout];
    self.smsCloudHistoryCollectionView.backgroundColor = [UIColor clearColor];
    self.smsCloudHistoryCollectionView.dataSource = self;
    self.smsCloudHistoryCollectionView.delegate = self;
    [self.smsCloudHistoryCollectionView registerClass:[SmsCloudAearHistoryCollectionCell class] forCellWithReuseIdentifier:@"SmsCloudAearHistoryCollectionCell"];
    [self addSubview:self.smsCloudHistoryCollectionView];
    [self.smsCloudHistoryCollectionView  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewWidth]));
        make.top.equalTo(smsCloudHistoryHeadLab.mas_bottom).offset([SmsCloudHead SmsCloudScreewRatio:30]);
        make.bottom.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
    }];
}
-(void)setSmsCloudResultAears:(NSArray *)smsCloudResultAears{
    _smsCloudResultAears = smsCloudResultAears;
    [self.smsCloudHistoryCollectionView reloadData];
}
- (void)smsCloudHistoryDelect{
    NSMutableArray * smsCloudResults = [[NSMutableArray alloc] init];
    [[NSUserDefaults standardUserDefaults] setObject:smsCloudResults.copy forKey:@"SmsCloudResultAear"];
    [SVProgressHUD showInfoWithStatus:@"已清空"];
    self.smsCloudResultAears = smsCloudResults.copy;
}
#pragma mark - UICollectionViewDelegateFlowLayout method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _smsCloudResultAears.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString * smsCloud = _smsCloudResultAears[indexPath.row];
    NSDictionary *collectDic = @{NSFontAttributeName : [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:28] weight:UIFontWeightRegular]};
    CGFloat collectWidth = [smsCloud boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,[SmsCloudHead SmsCloudScreewRatio:60]) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:collectDic context:nil].size.width+[SmsCloudHead SmsCloudScreewRatio:38];
    return CGSizeMake(collectWidth, [SmsCloudHead SmsCloudScreewRatio:60]);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return [SmsCloudHead SmsCloudScreewRatio:24];
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return [SmsCloudHead SmsCloudScreewRatio:20];
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SmsCloudAearHistoryCollectionCell *smsCloudAearHistoryCollectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SmsCloudAearHistoryCollectionCell" forIndexPath:indexPath];
    smsCloudAearHistoryCollectionCell.smsCloudAearHistory = _smsCloudResultAears[indexPath.row];
    return smsCloudAearHistoryCollectionCell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString * smsCloud = _smsCloudResultAears[indexPath.row];
    if(self.SmsCloudSearchBlock){
        self.SmsCloudSearchBlock(smsCloud);
    }
}
@end
